using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Rhino.Mocks;
using WWB.SitePointPrivateMessages.Core.Infrastructure.Logging;
using WWB.SitePointPrivateMessages.Core.Model.DataImport;
using WWB.SitePointPrivateMessages.Core.Model.Messages;
using WWB.SitePointPrivateMessages.Core.Service;

namespace WWB.SitePointPrivateMessages.Tests.DataImportTests
{
    [TestFixture]
    public class ExporterTests
    {
        private PrivateMessage pm1;
        private PrivateMessage pm2;
        private PrivateMessage pm3;

        private Folder sentItems;
        private Folder inbox;

        private SitePointMessageExport theExport;

        [TestFixtureSetUp]
        public void ArrangeAndAct()
        {
            pm1 = new PrivateMessage()
                      {
                          DateStamp = DateTime.Parse("2006-11-14 01:24"),
                          Title = "added iframe",
                          FromUser = new ForumUser() {Id = 83449, Name = "SampleMember"},
                          ToUsers = new List<ForumUser>() {new ForumUser() {Name = "FormAlly"}},
                          Body =
                              @"If you can give me a link to the site I'll take a look at what's being added and look at what kind of user input stuff it has that should be checked. Not that I'm an expert, but it's worth a try.
SampleMember

"
                      };

            pm2 = new PrivateMessage()
                      {
                          DateStamp = DateTime.Parse("2006-11-17 19:47"),
                          Title = "Re: ideas",
                          FromUser = new ForumUser() {Id = 83449, Name = "SampleMember"},
                          ToUsers = new List<ForumUser>() {new ForumUser() {Name = "FormAlly"}},
                          Body =
                              @"Good to hear you figured out what happened. And thanks for the link, interesting read. Now you just have to clean up the mess."
                      };
            pm3 = new PrivateMessage()
                      {
                          DateStamp = DateTime.Parse("2010-03-10 08:49"),
                          Title = "Congratulatiosn!",
                          FromUser = new ForumUser() {Id = 291251, Name = "Rubix3"},
                          ToUsers =
                              new List<ForumUser>()
                                  {
                                      new ForumUser() {Name = "SampleMember"},
                                      new ForumUser() {Name = "AdvisorSP"}
                                  },
                          Body = "Just noticed you turned green, hope you aren't ill :D"
                      };

            sentItems = new Folder("Sent Items", new[] {pm1, pm2});
            inbox = new Folder("Inbox", new[] {pm3});

            MessageDatabase mdb = new MessageDatabase(MockRepository.GenerateStub<IMessageSearcher>(), new [] {sentItems, inbox}, sentItems.GetUsersInFolder().Union(inbox.GetUsersInFolder()));

            theExport = mdb.ExportMessages(MockRepository.GenerateStub<ILogger>());

        }

        [Test]
        public void Export_is_not_null_and_has_right_number_of_folers()
        {
            Assert.IsNotNull(theExport);
            Assert.That(theExport.Folders.Count(), Is.EqualTo(2));
        }

        [Test]
        public void Export_has_right_messages()
        {
            var xsent = theExport.Folders[0];
            xsent.Messages[0].AssertPrivateMessageMatches(pm1);
            xsent.Messages[1].AssertPrivateMessageMatches(pm2);
            Assert.That(xsent.Messages.Count(), Is.EqualTo(2));

            var xinbox = theExport.Folders[1];
            xinbox.Messages[0].AssertPrivateMessageMatches(pm3);
            Assert.That(xinbox.Messages.Count(), Is.EqualTo(1));
        }
    }


    [TestFixture]
    public class ImporterTests
    {
        private ExportedMessage m1;
        private ExportedMessage m2;
        private ExportedMessage m3;

        private ExportedFolder exportedSentItems;
        private ExportedFolder exportedInbox;

        private SitePointMessageExport theExport;

        private MessageDatabase theResult;

        [TestFixtureSetUp]
        public void ArrangeAndAct()
        {
            m1 = new ExportedMessage()
                                       {
                                           DateStamp = "2006-11-14 01:24",
                                           Title = "added iframe",
                                           FromUser = "SampleMember",
                                           FromUserId = 83449,
                                           ToUser = "FormAlly",
                                           MessageBody =
                                               @"If you can give me a link to the site I'll take a look at what's being added and look at what kind of user input stuff it has that should be checked. Not that I'm an expert, but it's worth a try.
SampleMember

"
                                       };
            m2 = new ExportedMessage()
                                       {
                                           DateStamp = "2006-11-17 19:47",
                                           Title = "Re: ideas",
                                           FromUser = "SampleMember",
                                           FromUserId = 83449,
                                           ToUser = "FormAlly",
                                           MessageBody = @"Good to hear you figured out what happened. And thanks for the link, interesting read. Now you just have to clean up the mess."
                                       };
            m3 = new ExportedMessage()
                                       {
                                           DateStamp = "2010-03-10 08:49",
                                           Title = "Congratulatiosn!",
                                           FromUser = "Rubix3",
                                           FromUserId = 291251,
                                           ToUser = "SampleMember, AdvisorSP",
                                           MessageBody = "Just noticed you turned green, hope you aren't ill :D"

                                       };

            exportedSentItems = new ExportedFolder()
                            {
                                Name = "Sent Items",
                                Messages = new[] {m1, m2}
                            };
            exportedInbox = new ExportedFolder()
                                {
                                    Name = "Inbox",
                                    Messages = new[] {m3}
                                };
            theExport = new SitePointMessageExport()
                            {
                                Folders = new[] {exportedSentItems, exportedInbox}
                            };
            theResult = theExport.ImportMessages(MockRepository.GenerateStub<IMessageSearcher>(), MockRepository.GenerateStub<ILogger>());
        }

        [Test]
        public void Returned_is_not_null()
        {
            Assert.That(theResult, Is.Not.Null);
        }

        [Test]
        public void Returned_has_correct_folders()
        {
            Assert.That(theResult.Folders.Count(), Is.EqualTo(2));
            var sent = theResult.Folders.FirstOrDefault(x => x.Name == "Sent Items");
            Assert.That(sent, Is.Not.Null);
            Assert.That(sent.Name, Is.EqualTo("Sent Items"));
            Assert.That(sent.Messages.Count(), Is.EqualTo(2));
            m1.AssertPrivateMessageMatchesWithFolder(sent.Messages[0], "Sent Items");
            m2.AssertPrivateMessageMatchesWithFolder(sent.Messages[1], "Sent Items");
            var inbox = theResult.Folders.FirstOrDefault(x => x.Name == "Inbox");
            Assert.That(inbox, Is.Not.Null);
            Assert.That(inbox.Name, Is.EqualTo("Inbox"));
            Assert.That(inbox.Messages.Count(), Is.EqualTo(1));
            m3.AssertPrivateMessageMatchesWithFolder(inbox.Messages[0], "Inbox");
        }
    }
}